<?php
/*
http://image.spreadshirt.net/image-server/image/product/9786790/view/1/producttypecolor/1/type/png/width/190/height/190
http://429259.spreadshirt.net/de/DE/Shop/Index/index/page/2
*/

/**
 * Plugin spreadshirt: Link to your SpreadShirt-Shop
 * 
 * @license    GNU
 * @author     Juergen A.Lamers <jaloma.ac@googlemail.com>
 */

if (!defined('DOKU_INC'))
	define('DOKU_INC', realpath(dirname(__FILE__) . '/../../../') . '/');
if (!defined('DOKU_PLUGIN'))
	define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once (DOKU_PLUGIN . 'syntax.php');

/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */

class syntax_plugin_spreadshirt extends DokuWiki_Syntax_Plugin {

	/**
	 * return some info
	 */
	function getInfo() {
		return array (
			'author' => 'Juergen A.Lamers',
			'email' => 'jaloma.ac@googlemail.com',
			'date' => '2009-02-04',
			'name' => 'spreadshirt -- SpreadShirt-Shop Plugin',
			'desc' => 'Link to your SpreadShirt http://www.spreadshirt.net/',
			'url' => 'http://wiki.dokuwiki.org/plugin:spreadshirt',
		);
	}

	/**
	 * What kind of syntax are we?
	 */
	function getType() {
		return 'substition';
	}

	/**
	 * What about paragraphs? (optional)
	 */
	function getPType() {
		return 'block';
	}

	/**
	 * Where to sort in?
	 */
	function getSort() {
		return 999;
	}

	/**
	 * Connect pattern to lexer
	 */
	function connectTo($mode) {
		$this->Lexer->addSpecialPattern('~~SPREADSHIRT.*?~~', $mode, 'plugin_spreadshirt');
	}

	/**
	 * Handle the match
	 */
	function handle($match, $state, $pos, & $handler) {
		global $conf;
		$resultStr = '';
		$paramsArr = explode('~', $match);
		for ($i = 0; $i < count($paramsArr); $i++) {
			$paramPairArr = explode('=', $paramsArr[$i]);
			switch ($paramPairArr[0]) {
				case 'SPREADSHIRT' :
					break;
				case '' :
					break;
				case 'url' :
					$data['url'] = htmlspecialchars($paramPairArr[1], ENT_QUOTES, 'UTF-8');
					break;
				case 'img' :
					$data['img'] = htmlspecialchars($paramPairArr[1], ENT_QUOTES, 'UTF-8');
					break;				
				default :
//					$resultStr .= ' ' . $paramPairArr[0] . '="' . $paramPairArr[1] . '"';
					break;
			}
		}
		return $data;
	}


	/**
	 * Create output
	 */
	function render($mode, & $renderer, $data) {
		global $conf;
		if ($mode == 'xhtml') {
			list($data) = $data;
			$txt = '';
			if (isset($data['url'])) {
				$url = $data['url'];
			} else {
				$url = $this->getConf('spreadshirt_url');
			}
			if (isset($data['img'])) {
				$img = $data['img'];
			} else {
				$img = $this->getConf('spreadshirt_img');
			}
			$txt .= '<'.$this->getConf('spreadshirt_header_level_intro').'>'.
				$this->getConf('spreadshirt_header_text').
				'</'.$this->getConf('spreadshirt_header_level_extro').'>';
			$txt .= '<a href="'.$url.'"';
			$target = $this->getConf('spreadshirt_href_target');
			if (isset($target) && $target != '') {
	    		$txt .= ' target="'.$target.'">';
			}
			$txt .= '>';
			$txt .= '<img src="'.$img.'" alt="SpreadShirt" border="0" />';
			$txt .= '</a>';
			$renderer->doc .= $txt;
			return true;
		}
		return false;
	}
} // Class
